package com.chinacreator.asp.comp.sys.oauth2.sso.client.spi;

import java.io.IOException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.chinacreator.asp.comp.sys.oauth2.common.Credential;
import com.chinacreator.asp.comp.sys.oauth2.common.spi.OAuth2SSOSpi;
import com.supermap.application.common.UserFromKC;

public class SysMgrOAuth2SSOFilterSpiImpl implements OAuth2SSOSpi {



    @Override
    public void onAuthenticateSuccessHandler(HttpServletRequest request,
                                             HttpServletResponse response, FilterChain chain,
                                             Credential credential) throws IOException, ServletException {
        try {
            System.out.println("-----");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTokenCheckSuccess(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain chain,
                                    Credential credential) {


        try {

            //获取科创用户名与ID
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
            System.out.println(sdf.format(new Date()) + "\n SysMgrOAuth2SSOFilterSpiImpl \n统一登录科创用户名：" + credential.getUserInfo().getName());
            String name = credential.getUserInfo().getName();
            String id = credential.getUserInfo().getId();
            String accessToken = credential.getAccessToken();

            UserFromKC.setNAME(name);
            UserFromKC.setPASSWORD(id);


            System.out.println("统一登录科创用户名：" + name);
            System.out.println("统一登录获取后用户名：" + UserFromKC.getNAME());
            System.out.println("================" + "accessToken" + "=============" + accessToken);
            System.out.println("统一用户登录，请求成功");
            //页面跳转
            chain.doFilter(request,response);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
        try {
            System.out.println("-----");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onServerLogoutHandler(HttpServletRequest request,
                                      HttpServletResponse response, FilterChain chain,
                                      String redirectLoginUrl) throws IOException, ServletException {
        System.out.println("-----");
        System.out.println("统一用户登录，请求失败");
    }

    @Override
    public void onTokenCheckFailed(HttpServletRequest request,
                                   HttpServletResponse response, FilterChain chain,
                                   String redirectLoginUrl) throws IOException, ServletException {
        System.out.println("-----");

        /*UserFromKC.setNAME("admin");
        UserFromKC.setPASSWORD("admin");*/

        System.out.println("统一用户登录，请求失败");
    }

    @Override
    public void onServerFailedHandler(HttpServletRequest request,
                                      HttpServletResponse response, Exception e, FilterChain chain,
                                      String redirectLogoutUrl) throws IOException, ServletException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write(e.getMessage());
        ((HttpServletResponse) response)
                .setStatus(HttpServletResponse.SC_FORBIDDEN);
        if (null != redirectLogoutUrl && !redirectLogoutUrl.trim().equals("")) {
            ((HttpServletResponse) response).setHeader("refresh", "3;url="
                    + redirectLogoutUrl);
            response.getWriter().write(
                    "<br/>3秒后自动跳转，如果跳转失败请点击<a href='" + redirectLogoutUrl
                            + "'>重新登录</a>");
        }
    }
}
